package top.lyf1210.yuwenpicturebackend.controller;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import top.lyf1210.yuwenpicturebackend.annotation.AuthCheck;
import top.lyf1210.yuwenpicturebackend.common.BaseResponse;
import top.lyf1210.yuwenpicturebackend.common.ResultUtils;
import top.lyf1210.yuwenpicturebackend.constant.UserAuthConstant;
import top.lyf1210.yuwenpicturebackend.exception.BusinessException;
import top.lyf1210.yuwenpicturebackend.exception.ErrorCode;
import top.lyf1210.yuwenpicturebackend.exception.ThrowUtils;
import top.lyf1210.yuwenpicturebackend.model.User;
import top.lyf1210.yuwenpicturebackend.model.dto.user.*;
import top.lyf1210.yuwenpicturebackend.model.vo.user.LoginUserVO;
import top.lyf1210.yuwenpicturebackend.model.vo.user.UserVO;
import top.lyf1210.yuwenpicturebackend.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static top.lyf1210.yuwenpicturebackend.utils.EncryptUtils.getEncryptPassWord;

/**
 * @ClassNameUserController
 * @Description 用户控制层
 * @Author钰玟
 * @Date2025/4/10 下午8:42
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
//@Api(tags = "用户相关接口")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterDTO 注册数据传输模型
     * @return 用户Id
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
        String userAccount = userRegisterDTO.getUserAccount();
        String userPassword = userRegisterDTO.getUserPassword();
        String userCheckPassword = userRegisterDTO.getCheckPassword();
        long result = userService.userRegister(userAccount, userPassword, userCheckPassword);
        return ResultUtils.success(result);
    }

    /**
     * 用户登录
     * @param userLoginDTO 用户登录数据传输模型
     * @return 用户登录展示模型
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request){
        ThrowUtils.throwIf(userLoginDTO==null, ErrorCode.PARAMS_ERROR);
        String userAccount = userLoginDTO.getUserAccount();
        String userPassword = userLoginDTO.getUserPassword();
        LoginUserVO userLoginVO = userService.userLogin(userAccount, userPassword,request);
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 获取用户
     * @param request request
     * @return loginVO
     */
    @GetMapping("/get/login")
    @ApiOperation("获取登录用户")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(userService.getLoginUserVO(loginUser));
    }

    /**
     * 用户注销
     * @param request 请求
     * @return boolean
     */
    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request){
        ThrowUtils.throwIf(request==null, ErrorCode.PARAMS_ERROR);
        Boolean result = userService.userLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 增加用户
     * @param userAddDTO 增加用户的传输对象
     * @return long
     */
    @PostMapping("/add")
    @ApiOperation("新增用户")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Long> userAdd(@RequestBody UserAddDTO userAddDTO){
        ThrowUtils.throwIf(userAddDTO==null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        // 拷贝基本属性
        BeanUtil.copyProperties(userAddDTO,user);
        //赋值其他属性
        //默认密码
        String password = "12345678";
        String encryptPassword = getEncryptPassWord(password);
        user.setUserPassword(encryptPassword);
        boolean result  = userService.save(user);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(user.getId());
    }

    /**
     * 查询用户
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/get")
    @ApiOperation("根据id查询用户")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(@RequestParam("id") Long id){
        ThrowUtils.throwIf(id<=0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwIf(user==null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 获取 脱敏用户
     * @param id 用户id
     * @return 脱敏用户对象
     */
    @GetMapping("/get/vo")
    @ApiOperation("根据id查询脱敏用户")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<UserVO> getUserVOById(@RequestParam("id") Long id){
        BaseResponse<User> response = getUserById(id);
        User user = response.getData();
        return ResultUtils.success(userService.getUserVO(user));
    }
    /**
     * 更新用户
     */
    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateDTO userUpdateDTO) {
        if (userUpdateDTO == null || userUpdateDTO.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO, user);
        boolean result = userService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 分页查询
     * @param userQueryDTO 查询DTO
     * @return
     */
    @PostMapping("/list/page/vo")
    @ApiOperation("获取分页用户列表")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> listUserVOByPage(@RequestBody UserQueryDTO userQueryDTO){
        ThrowUtils.throwIf(userQueryDTO==null, ErrorCode.PARAMS_ERROR);
        // 获取分页相关数据
        long current = userQueryDTO.getCurrent();
        long pageSize = userQueryDTO.getPageSize();
        //分页查询
        Page<User> userPage = userService.page(new Page<>(current,pageSize),
                userService.getUserQueryWrapper(userQueryDTO));
        Page<UserVO> userVOPage = new Page<>(current,pageSize,userPage.getTotal());
        List<UserVO> userVOList = userService.getUserVOList(userPage.getRecords());
        userVOPage.setRecords(userVOList);
        return ResultUtils.success(userVOPage);
    }

    /**
     * 根据id删除用户
     * @param id 用户id
     * @return boolean
     */
    @PostMapping("/delete/{id}")
    @ApiOperation("删除用户")
    @AuthCheck(mustRole = UserAuthConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@PathVariable("id") Long id){
        ThrowUtils.throwIf(id<=0, ErrorCode.PARAMS_ERROR);
        boolean result = userService.removeById(id);
        return ResultUtils.success(result);
    }
}
